<?php
namespace app\common\logic;

use app\common\service\App;
use think\Db;
use think\Model;


class Users extends Model
{


    /**
     * 后台修改用户信息
     */
    public function saveChange($data)
    {

        $data['update_time'] = time();
        if (is_numeric($data['id'])) {
            $validate = validate('users');
            if (!$validate->scene('back_edit')->check($data)) api_return(0, $validate->getError());
            if (!empty($data['password'])) {
                $salt = $this->where('user_id', $data['id'])->value('salt');
                $data['password'] = md5(md5($data['password']).$salt);
            } else {
                unset($data['password']);
            }
            $data['token_expire'] = 0;
            return $this->allowField(true)->save($data, [$this->getPk() => $data['id']]);
        } else {
            $lastId = $this->order('user_id desc')->value('user_id');
            $data['uuid'] =  $uuid = 101088888+$lastId*1000+rand(1,999);
            $validate = validate('users');
            if (!$validate->scene('back_add')->check($data)) api_return(0, $validate->getError());
            $data['header_img'] = !empty($data['header_img']) ? $data['header_img'] : config('default_img');
            if (empty( $data['nick_name'])){
                $data['nick_name']  = '伴心用户'.substr($data['phone'],-4);
            }

            if (empty($data['sign'])){
                $data['sign']= '你不主动，我们之间怎么会有故事～';
            }

            if (empty($data['sex']) || $data['sex'] == 1){
                $data['sex'] = 1;
                if (App::getExplain('pay_switch') === 'true'){

                    $data['is_pay'] = 0;

                }else{

                    $data['is_pay'] = 1;

                }
                $data['is_real'] = 1;
            }else{
                $data['sex']     = 2;
                $data['is_pay']  = 1;
                $data['is_real'] = 0;
            }

            $data['salt'] = generateStr(); //密码盐
            if (empty($data['password'])){
                $data['password'] = '123456';
            }
            $data['password'] = md5(md5($data['password']).$data['salt']);
            $data['create_time'] = time();
            $result =  $this->allowField(true)->save($data);
            if ($result) {

                $data['user_id'] = $this->getLastInsID();
                $data['city'] = ipToCity(request()->ip());
                $data['last_login_time'] = time();
                //增加背包数量为0数据
                $item['user_id'] = $data['user_id'];
                $item['key']     = 0;
                $item['num']     = 0;
                for ($i = 4;$i <= 6;$i++){
                    $item['type'] = $i;
                    Db::name('my_package')->insert($item);
                }
                
                //注册user_extend

                Db::name('user_extend')->strict(false)->insert($data);

            }
            return $result;
        }
    }

    /**
     * 前端修改用户信息
     */
    public function change($data)
    {

        if (is_numeric($data['id'])) {
            $validate = validate('users');
            if (!$validate->scene('edit')->check($data)) api_return(0, $validate->getError());
            if (!empty($data['password'])) {
                $salt = $this->where('user_id',$data['id'])->value('salt');
                $data['password'] = md5(md5($data['password']).$salt);
            } else {
                unset($data['password']);
            }
            return $this->allowField(true)->save($data, ['user_id' => $data['id']]);
        } else {
            $lastId = $this->order('user_id desc')->value('user_id');
            $data['uuid'] =  $uuid = 101088888+$lastId*1000+rand(1,999);
            $validate = validate('base');
            if (!$validate->scene('front_user_add')->check($data)) api_return(0, $validate->getError());
            $data['header_img'] = config('default_img');
            if (empty( $data['nick_name'])){
                $data['nick_name']  = '伴心用户'.substr($data['phone'],-4);
            }
            if (empty($data['password'])){
                $data['password'] = '123456';
            }

            if (empty($data['sign'])){
                $data['sign']= '你不主动，我们之间怎么会有故事～';
            }

            if (empty($data['sex']) || $data['sex'] == 1){
                $data['sex'] = 1;
                if (App::getExplain('pay_switch') === 'true'){

                    $data['is_pay'] = 0;

                }else{

                    $data['is_pay'] = 1;

                }
                $data['is_real'] = 1;
            }else{
                $data['sex']     = 2;
                $data['is_pay']  = 1;
                $data['is_real'] = 0;
            }

            $data['create_time'] = time();
            $data['update_time'] = time();

            $data['salt'] = generateStr(); //密码盐
            $data['password'] = md5(md5($data['password']).$data['salt']);



            $result = $this->allowField(true)->save($data);
            if ($result){

                $data['city'] = ipToCity(request()->ip());
                $data['user_id'] = $this->getLastInsID();
                $data['last_login_time'] = time();
                //真人认证图片
                if (!empty($data['girl_img'])){

                    Db::name('girl_img')->strict(false)->insert($data);
                }

                //增加背包数量为0数据
                $item['user_id'] = $data['user_id'];
                $item['key']     = 0;
                $item['num']     = 0;
                for ($i = 4;$i <= 6;$i++){
                    $item['type'] = $i;
                    Db::name('my_package')->insert($item);
                }

                //注册user_extend
                Db::name('user_extend')->strict(false)->insert($data);

            }
            return $result;
        }
    }

    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 第三方登录创建角色
     */
    public function userAdd($data)
    {
        $data['city'] = ipToCity(request()->ip());
        $validate = validate('base');

        if (empty($data['sex']) || $data['sex'] == 1){
            $data['sex'] = 1;
            if (App::getExplain('pay_switch') === 'true'){

                $data['is_pay'] = 0;

            }else{

                $data['is_pay'] = 1;

            }
            $data['is_real'] = 1;
        }else{
            $data['sex']     = 2;
            $data['is_pay']  = 1;
            $data['is_real'] = 0;
        }

        if (!$validate->scene('third_user_add')->check($data)) api_return(0, $validate->getError());
        $result =  $this->allowField(true)->save($data);
        if ($result){

            $data['user_id'] = $this->getLastInsID();
            $data['last_login_time'] = time();
            //增加背包数量为0数据
            $item['user_id'] = $data['user_id'];
            $item['key']     = 0;
            $item['num']     = 0;
            for ($i = 4;$i <= 6;$i++){
                $item['type'] = $i;
                Db::name('my_package')->insert($item);
            }
            $data['city'] = ipToCity(request()->ip());
            //注册user_extend
            Db::name('user_extend')->strict(false)->insert($data);
            //修改用户uuid
            $uuid = 101088888+$data['user_id']*1000+rand(1,999);
            $this->where('user_id',$data['user_id'])->update(['uuid'=>$uuid]);

        }
        return $result;
    }



}